package my.engine.core;

import my.engine.utils.SettingsUtil;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * User: emikolenko
 * Date: 06.03.2008
 * Time: 13:28:31
 */
public class MainPageRedirector extends HttpServlet {

    protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
        doGet(httpServletRequest, httpServletResponse);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String lang;
        if (session.getAttribute(Consts.lang) != null && session.getAttribute(Consts.lang).toString().length() > 0) {
            lang = session.getAttribute(Consts.lang).toString();
        } else {
            lang = SettingsUtil.getProperty(Consts.defLang);
        }
        String path = request.getRequestURI().substring(request.getContextPath().length());
        if (path == null || (!path.startsWith("/public") && !path.startsWith("/jsp") && !path.startsWith(request.getContextPath() + "/jsp")
        && ! path.startsWith("/css") && !path.startsWith("/js")) && !path.startsWith("/services")) {
            response.sendRedirect(request.getContextPath() + "/public/" + lang + "/" + FlowManager.defaultUrl);
        }
    }
}
